/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView;


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TScheduled;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.OrderScheduledModel;
import com.ruipengkj.service.TScheduledService;
import com.ruipengkj.view.base.BasePagePanel;


/**在线预定订单
 * @Author：pinea
 * @Date：2012-03-14
 */
public class OrderScheduledPanel extends BasePagePanel<TScheduled> implements Runnable{

	private static final long serialVersionUID = 1L;
	private JLabelJTextFieldComponent orderNum; //预订单号
	private JLabelJTextFieldComponent userNum; //用户编号
	private DataPackageComponent startTime; //开始时间
	private DataPackageComponent endTime;	//结束时间
	
	private JTable table;
	private OrderScheduledModel orderScheduledModel;
	private JButton queryButton;//查询按钮
	private JButton clearButton;//清除按钮
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TScheduled> pageView;//翻页主键
	private JScrollPane tableJsp;
	private TScheduledService tScheduledService;
	
	public OrderScheduledPanel(){
		super();
		this.setLayout(new BorderLayout());
		this.initView();
		this.initListener();
		tScheduledService=BuildSpringApplication.getInstance().getTScheduledServiceImpl();
	}
	
	private void initView(){
		pageView=new PageView<TScheduled>();
		orderScheduledModel=new OrderScheduledModel();
		table=uiFactory.createRPJTable(orderScheduledModel, null, this, 50, JLabel.CENTER);
//		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		this.add(topPanel(),BorderLayout.NORTH);
		this.add(tableJsp,BorderLayout.CENTER);
		this.add(pagingComponent,BorderLayout.SOUTH);
		//排序
		TableRowSorter<OrderScheduledModel> tableRowSorter = new TableRowSorter<OrderScheduledModel>(orderScheduledModel);  
		table.setRowSorter(tableRowSorter);
	}
	private JPanel topPanel(){
		QueryPanel queryPanel=new QueryPanel("在线预订单",ImageFactory.loadImage(ImageFactory.FUN_1_ICON_3));
		JPanel top_jp=new JPanel();
		top_jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		orderNum=new JLabelJTextFieldComponent("预订编号：", 15);
		userNum=new JLabelJTextFieldComponent("用户编号：", 15);
		startTime=new DataPackageComponent("预订时间：", 10);
		endTime=new DataPackageComponent("时间：", 10);
		queryButton=new RPJButtonBlue("查询");
		clearButton=new RPJButtonGray("清空");
		top_jp.add(orderNum);
		top_jp.add(userNum);
		top_jp.add(startTime);
		top_jp.add(endTime);
		top_jp.add(queryButton);
		top_jp.add(clearButton);
		queryPanel.addPanel(top_jp);
//		queryPanel.addBoxPanel(top_jp);
		return queryPanel;
	}

	private void initListener(){
		queryButton.addActionListener(this);
		orderNum.getText().addKeyListener(this);
		userNum.getText().addKeyListener(this);
		clearButton.addActionListener(this);
	}
	
	@Override
	public void run() {
		this.termQuery(1);
	}
	//条件查询
	@Override
	protected void termQuery(final int currentPage) {
		final TScheduled tScheduled=new TScheduled();
		tScheduled.setScheduledID(orderNum.getText().getText().trim());
		tScheduled.setHappenTime(startTime.getDate());
		tScheduled.setEndTime(endTime.getDate());
		tScheduled.setStatus(ApplicationContext.ORDER_STATUS1);
		TUser tUser=new TUser();
		tUser.setUserID(userNum.getText().getText().trim());
		tScheduled.setTUser(tUser);
		
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TScheduled> pageModel=tScheduledService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tScheduled);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						orderScheduledModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								pagingComponent.show();
								TabelUtil.setJTabelAutoClumnWidth(table);
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						orderScheduledModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TScheduled> getPageView() {
		return pageView;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询
			this.termQuery(1);
		} else if (e.getSource() == clearButton) {
			orderNum.getText().setText(""); //预订单号
			userNum.getText().setText(""); //用户编号
			startTime.setDate(null); //开始时间
			endTime.setDate(null);	//结束时间
		} 
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getSource()==orderNum.getText()||e.getSource()==userNum.getText()){
			//查询
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				queryButton.doClick();	
			}
		}
	}

	@Override
	public void clear() {
		orderScheduledModel.clear();
		orderNum.getText().setText(""); //预订单号
		userNum.getText().setText(""); //用户编号
		startTime.setDate(null); //开始时间
		endTime.setDate(null);	//结束时间
		
	}
}
